<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\AICS\AICSEnums;
use IDock\Insurance\AICS\Response\AllAICSResponse;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class AccidentInsuranceResponse extends AllAICSResponse
{

    public $policy = '';

    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = str_replace('&lt;','<',$body);
        $body = str_replace('&gt;','>',$body);
        $body = str_replace("'",'"',$body);
        $start = strpos($body,'<response>');
        $end = strpos($body,'</response>');
        $body = substr($body,$start + 10,intval($end - $start -10));
        if ($body) {
            libxml_disable_entity_loader(false);

            $body = simplexml_load_string($body);
            $body = $this->objectToArray($body);
            $errorCode = $body['head']['responseCompleteMessageStatus']['messageStatusCode'] ?? '';
            $message = $body['head']['responseCompleteMessageStatus']['messageStatusDescription'] ?? '未知错误';
            $this->setBody($body);
            if ($errorCode !== 'E0000') {
                $this->setError($message);
                return $this;
            }
        }

        return $this;
    }

    public function printerInit(): Response
    {
        $data = $this->getBody()['head'] ?? '';

        $str = $data['responseCompleteMessageStatus']['messageStatusDescription'] ?? '';

        if(!empty($str)){
            $arr = explode('：',$str);
            $this->policy = $arr[1] ?? '';
        }

        return $this;
    }
}
